package com.aisile.manager.controller;


import com.aisile.entity.PageResult;
import com.aisile.entity.Result;
import com.aisile.pojo.TbBrand;
import com.aisile.sellergoods.service.BrandService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//@Reference(timeout=2000) 连接超时的设置
@RestController
@RequestMapping("brand")
public class BrandController {

    @Reference //远程调用dubbo
    BrandService brandService;
    @RequestMapping("findAll")
    public List<TbBrand> findAll(){
        List<TbBrand> all=brandService.findAll();

        return all;

    }

    /**
     * 分页的方法
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("findPage")
    public PageResult finPage(int page,int rows,@RequestBody TbBrand tbBrand){

       return brandService.findPage(page, rows,tbBrand);

    }

    /**
     * 保存方法
     * application/json 数据格式用requestbody 来接受数据
     * 路径的注解 @PathVariable
     *
     * @param tbBrand
     * @return
     */
    @RequestMapping("save")
    public Result save(@RequestBody TbBrand tbBrand){

        try{

            brandService.save(tbBrand);
            return new Result(true,"");

        }
        catch (Exception e){

            return new Result(false,"服务器出现异常");

        }

    }

    /**
     * 再接受参数时 可以要包装类Long @RequestParam(defaultValue="0")
     * 小写long 前台没有传id时，会发生空指针
     * @return
     */
    @RequestMapping("findOne")
    public TbBrand findOne(long id){

       return  brandService.findOne(id);
    }

    @RequestMapping("update")
    public Result update(@RequestBody TbBrand tbBrand){

        try{

            brandService.update(tbBrand);
            return new Result(true,"");

        }
        catch (Exception e){

            return new Result(false,"服务器出现异常");

        }

    }
    //通用异常处理时 ControllerAdvice(添强注解) ExceptionHandler(异常处理注解)
    @RequestMapping("dele")
    public Result dele(Long[] ids){

        try {
            brandService.dele(ids);
            return new Result(true,"");

        }catch (Exception e){

            return new Result(false,"服务器出现异常");
        }

    }

    @RequestMapping("findBrandList")
    public List<Map> findBrandList(){

       return brandService.findBrandList();
    }


}
